﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Engine.Sgml.Merging;
using System.Diagnostics;

namespace Engine.Sgml.Validation {
    internal sealed class BlockItemCollection : Collection<BlockItem> {
        public bool TryGetBlocks(SElement ancestor, out IList<InconsistentBlock> blocks) {
            for (int i = 0; i < Items.Count; i++) {
                if (object.ReferenceEquals(Items[i].Ancestor, ancestor)) {
                    blocks = Items[i].InconsistentBlocks;
                    return true;
                    }
                }
            blocks = null;
            return false;
            }

        public void Remove(SElement ancestor) {
            for (int i = 0; i < Items.Count; i++) {
                if (object.ReferenceEquals(Items[i].Ancestor, ancestor)) {
                    Items.RemoveAt(i);
                    return;
                    }
                }
            }

        public void Add(SElement ancestor, IList<InconsistentBlock> blocks) {
            Add(ancestor, blocks, false);
            }

        public void Add(SElement ancestor, IList<InconsistentBlock> blocks, bool uncompleted) {
            Debug.Assert(ancestor != null);
            Debug.Assert(blocks != null);
            base.Add(new BlockItem {
                AncestorUncompleted = uncompleted,
                Ancestor = ancestor,
                InconsistentBlocks = blocks
            });
            }

        public void RemoveAllocated() {
            for (int i = 0; i < Items.Count; i++) {
                if (Items[i].InconsistentBlocks.Count == 0)
                    Items.RemoveAt(i--);
                }
            }

        public bool HasUncompleted() {
            for (int i = 0; i < Items.Count; i++) {
                if (Items[i].AncestorUncompleted)
                    return true;
                }
            return false;
            }
        }
    }
